<!DOCTYPE html>
<html>

<head>
    <meta name="layout" content="main2"/>
    <g:set var="entityName" value="${message(code: 'address.label', default: 'address')}"/>
    <title>新增新增地址</title>
</head>

<body>

<div class="main-body">
    <div class="page-wrapper">
        <!-- Page-header start -->
        <div class="page-header">
            <div class="page-header-title">
                <h4>新增地址</h4>
            </div>

            <div class="page-header-breadcrumb">
                <ul class="breadcrumb-title">
                    <li class="breadcrumb-item">
                        <a href="/salesDashboard/index">
                            <i class="icofont icofont-home"></i>
                        </a>
                    </li>
                    <li class="breadcrumb-item">
                        <g:link action="index">新增地址列表</g:link>
                    </li>
                    <li class="breadcrumb-item"><a href="#!">新增新增地址</a>
                    </li>
                </ul>
            </div>
        </div>
        <!-- Page-header end -->
        <!-- Page-body start -->
        <div class="page-body">
            <g:if test="${flash.message}">
                <div class="message alert alert-info" role="status">${flash.message}
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">×</span></button>
                </div>
            </g:if>
            <g:hasErrors bean="${this.address}">
                <ul class="errors" role="alert">
                    <g:eachError bean="${this.address}" var="error">
                        <li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>>
                            <g:message error="${error}"/>
                        </li>
                    </g:eachError>
                </ul>
            </g:hasErrors>
        <!-- Hover table card start -->
            <div class="card">
                <div class="card-header">
                    <h5>新增新增地址</h5>

                    <div class="card-header-right">
                        <i class="icofont icofont-rounded-down"></i>
                    </div>
                </div>
               <div class="card-block">
                   <g:form action="save" class="form-horizontal">

                       <div class="form-group row">
                           <label class="col-md-2 control-label is-required">地址类型</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="type.id"
                                         value="${this.address?.type?.id}" from="${com.next.AddressType.list()}"
                                         optionKey="id" optionValue="name" noSelection="${['': '请选择']}"></g:select>
                           </div>
                       </div>

                       <div class="hr-line-dashed"></div>

                       <div class="form-group row">
                           <label class="col-md-2 control-label is-required">地址</label>

                           <div class="col-md-3">
                               %{--<g:textField class="hidden" name="targetUri" value="${targetUri}"></g:textField>--}%
                               <input type="hidden" name="targetUri" value="${targetUri}">
                               <g:textField class="form-control" name="address" value="${this.address?.address}"></g:textField>
                           </div>
                           <label class="col-md-2 control-label">邮编</label>

                           <div class="col-md-3">
                               <g:textField class="form-control" name="zip"
                                            value="${this.address?.zip}"></g:textField>
                           </div>
                       </div>



                       <div class="form-group row">
                           <label class="col-md-2 control-label">国家</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="country.id" id="country"
                                         value="${this.address?.country?.id}" from="${com.next.Country.list()}"
                                         optionKey="id" optionValue="name" noSelection="${['': '请选择']}"></g:select>
                           </div>
                           <label class="col-md-2 control-label">省份</label>

                           <div class="col-md-3">
                               <select id="province" name="province.id" value="${this.address?.province?.id}" class="form-control provinceName">
                               </select>
                           </div>
                       </div>
                       <div class="form-group row">
                           <label class="col-md-2 control-label">城市</label>

                           <div class="col-md-3">
                               <select id="city" name="city.id" value="${this.address?.city?.id}" class="form-control cityName">
                               </select>
                           </div>

                           <label class="col-md-2 control-label">市区</label>

                           <div class="col-md-3">
                               <select id="district" name="district.id" value="${this.address?.district?.id}" class="form-control districtName">
                               </select>
                           </div>
                       </div>

                       <div class="form-group row" hidden>
                           <label class="col-md-2 control-label">客户</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="account.id"
                                         value="${this.address?.account?.id}" from="${com.next.Account.findAllByActive(true)}"
                                         optionKey="id" optionValue="name" noSelection="${['': '请选择']}"></g:select>
                           </div>
                           <label class="col-md-2 control-label">联系人</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="contact.id"
                                         value="${this.address?.contact?.id}" from="${com.next.Contact.findAllByActive(true)}"
                                         optionKey="id" optionValue="fullName" noSelection="${['': '请选择']}"></g:select>
                           </div>

                       </div>
                       <div class="form-group row" hidden>
                           <label class="col-md-2 control-label">市场</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="campaign.id"
                                         value="${this.address?.campaign?.id}" from="${com.next.Campaign.findAllByActive(true)}"
                                         optionKey="id" optionValue="name" noSelection="${['': '请选择']}"></g:select>
                           </div>
                           <label class="col-md-2 control-label">商机</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="opportunity.id"
                                         value="${this.address?.opportunity?.id}" from="${com.next.OpportunityTeam.findAllByUser(this?.user)}"
                                         optionKey="opportunityId" optionValue="opportunity" noSelection="${['': '请选择']}"></g:select>
                           </div>

                       </div>
                       <div class="hr-line-dashed" hidden></div>

                       <div class="form-group row" hidden>
                           <label class="col-md-2 control-label">线索</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="lead.id"
                                         value="${this.address?.lead?.id}" from="${com.next.Lead.list()}"
                                         optionKey="id" optionValue="fullName" noSelection="${['': '请选择']}"></g:select>
                           </div>
                           <label class="col-md-2 control-label">活动</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="activity.id"
                                         value="${this.address?.activity?.id}" from="${com.next.Activity.list()}"
                                         optionKey="id" optionValue="name" noSelection="${['': '请选择']}"></g:select>
                           </div>
                       </div>

                       <div class="form-group row" hidden>
                           <label class="col-md-2 control-label is-required">地址类型</label>
                           <label class="col-md-2 control-label is-required">合同</label>

                           <div class="col-md-3">
                               <g:select class="form-control" name="contract.id"
                                         value="${this.address.contract?.id}" from="${com.next.Contract.list()}"
                                         optionKey="id" optionValue="serialNumber" noSelection="${['': '请选择']}"></g:select>
                           </div>
                       </div>


                       <div class="form-group row">
                           <div class="col-md-12">
                               <g:submitButton class="btn btn-info" name="create" value="保存"/>
                           </div>
                       </div>
                   </g:form>
               </div>


            </div>
            <!-- Hover table card end -->
        </div>

        <!-- Page-body end -->

    </div>
</div>

<g:javascript>
    $(function () {

        function getProvinceByCountry() {
            $.ajax({
                type: "POST",
                url: "/address/getProvinceByCountry",
                data: {
                    country: $("#country").val(),
                },
                success: function (data) {
                    var provinceList = data.provinceList;
                    var option;
                    if (data.status == "success") {
                        $('.provinceName').html("");
                        $(".provinceName").append('<option value="">请选择</option>')
                        for (var i = 0; i < provinceList.length; i++) {
                            option = '<option value="' + provinceList[i].id + '">' + provinceList[i].name + '</option>';
                            $(".provinceName").append(option)
                        }
                        $("select").select2();
                    }
                },
                error: function () {
                    swal("获取失败，请稍后重试", "", "error");
                }
            });
        }

        function getCityByProvince() {
            $.ajax({
                type: "POST",
                url: "/address/getCityByProvince",
                data: {
                    province: $("#province").val(),
                },
                success: function (data) {
                    var cityList = data.cityList;
                    var option;
                    if (data.status == "success") {
                        $('.cityName').html("");
                        $(".cityName").append('<option value="">请选择</option>')
                        for (var i = 0; i < cityList.length; i++) {
                            option = '<option value="' + cityList[i].id + '">' + cityList[i].name + '</option>';
                            $(".cityName").append(option)
                        }
                        $("select").select2();
                    }
                },
                error: function () {
                    swal("获取失败，请稍后重试", "", "error");
                }
            });
        }

        function getDistrictByCity() {
            $.ajax({
                type: "POST",
                url: "/address/getDistrictByCity",
                data: {
                    city: $("#city").val(),
                },
                success: function (data) {
                    var districtList = data.districtList;
                    var option;
                    if (data.status == "success") {
                        $('.districtName').html("");
                        $(".districtName").append('<option value="">请选择</option>')
                        for (var i = 0; i < districtList.length; i++) {
                            option = '<option value="' + districtList[i].id + '">' + districtList[i].name + '</option>';
                            $(".districtName").append(option)
                        }
                        $("select").select2();
                    }
                },
                error: function () {
                    swal("获取失败，请稍后重试", "", "error");
                }
            });
        }

        getProvinceByCountry();
        getCityByProvince();
        getDistrictByCity();

        // 动态获取省、城市、区县
        $("#country").change(function () {
            getProvinceByCountry();
        });
        $("#province").change(function () {
            getCityByProvince();
        });
        $("#city").change(function () {
            getDistrictByCity();
        });
    })
</g:javascript>

</body>

</html>
